import {defHttp} from '/@/utils/http/axios';
import {Modal} from 'ant-design-vue';

enum Api {
  list = '/codegen/codegenTable/list',
  edit='/codegen/edit',
  deleteOne = '/codegen/codegenTable/delete',
  deleteBatch = '/codegen/codegenTable/deleteBatch',
  copyAndSave = '/codegen/codegenTable/copyAndSave',
  sync='/codegen/sync-from-db',
  importExcel = '/codegen/codegenTable/importExcel',
  exportXls = '/codegen/codegenTable/exportXls',
  codegenFormColumnList = '/codegen/codegenTable/queryCodegenFormColumnByMainId',
  validateFormColumnList = '/codegen/codegenTable/queryValidateColumnByMainId',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 查询子表数据
 * @param params
 */
export const codegenFormColumnList = Api.codegenFormColumnList;

/**
 * 查询子表数据
 * @param params
 */
export const validateFormColumnList = Api.validateFormColumnList;
/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
  defHttp.get({url: Api.list, params});

  
// 下载生成代码
export function downloadCodegen(data) {
  return defHttp.download({ url: '/codegen/download?tableId=' + data.id }, data.tableName + '.zip')
}

// 预览生成代码
export function previewCodegen(id: number) {
  return defHttp.get({ url: '/codegen/preview?tableId=' + id })
}

// 同步数据库字段
export function syncFromDb(data,handleSuccess) {
  return defHttp.get({ url: Api.sync+'?tableId='+data.id}).then(() =>{
    handleSuccess();
  })
}

/**
 * 删除单个
 * @param params
 * @param handleSuccess
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

/**
 * 复制新增
 * @param params
 * @param handleSuccess
 */
export const copyAndSave = (params,handleSuccess) => {
  return defHttp.delete({url: Api.copyAndSave, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除
 * @param params
 * @param handleSuccess
 */
export const batchDelete = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}
/**
 * 更新
 * @param params
 */
export const update = (params) => {
  return defHttp.post({url: Api.edit, params});
}


/**
 * 保存高级配置
 * @param params
 */
export const changeSeniorConfig = (params) => {
  return defHttp.post({url: Api.edit, params});
}



// 获得表定义
export function getSchemaTableList(params) {
  return defHttp.get({ url: '/codegen/db/table/list', params })
}

// 基于数据库的表结构，创建代码生成器的表定义
export function createCodegenList(data) {
  return defHttp.post({ url: '/codegen/create-list', data })
}
